package com.hmsm.server.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hmsm.sys.bean.LocalUser;


public class BaseController {

	/**
	 * 
	 *  @描述:设置用户到session
	 *  @方法名称:setSessionUser
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午03:49:59 
	 *  @param user
	 *
	 */
	public<U extends LocalUser> void setSessionUser(U user, HttpSession session) {
		session.setAttribute("user", user);
	}

	/**
	 * 
	 *  @描述:从session中获取用户
	 *  @方法名称:getSessionUser
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午03:50:14 
	 *  @return
	 *
	 */
	public<U extends LocalUser> U getSessionUser(HttpSession session) {
		Object obj = session.getAttribute("user");
		if (obj == null) {
			return null;
		}
		return (U) obj;
	}
	
	public void removeSessionUser(HttpSession session){
		session.removeAttribute("user");
	}

	/**
	 * 
	 *  @描述:获取用户请求的地址
	 *  @方法名称:getHttpAddress
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午03:51:48 
	 *  @return
	 *
	 */
	public String getHttpAddress(HttpServletRequest request) {
		String url = request.getScheme() + "://";
		url += request.getHeader("host");
		url += request.getRequestURI();
		if (request.getQueryString() != null) {
			url += "?" + request.getQueryString();
		}
		return url;
	}
	
}
